
function convertTime(str) {
    let ss = str.split(":");
    if (ss.length == 2) {
        return parseInt(ss[0] * 60 + parseInt(ss[1]));
    }
    return 0;
}

function learnNextClass() {
    let unstart = document.getElementsByClassName("content-unstart");
    if (unstart.length == 0) {
        unstart = document.getElementsByClassName("append-plugin-tip");
    }
    let min = 9999;
    let nexta = null;
    for (let i = 0; i < unstart.length; i++) {
        let n = unstart[i];
        if (n == null) {
            console.log("unstart[" + i + "]: is null");
            continue;
        }
        let na = n.parentElement;
        if (na == null) {
            console.log("unstart[" + i + "].parentElement is null");
            continue;
        }
        if (na.innerHTML.indexOf("学习中") > -1) continue;

        console.log(na.href);
        let ids = na.id.split("_");
        if (ids.length == 3) {
            let id = parseInt(ids[1]);
            if (id < min) {
                min = id;
                nexta = na;
            }
        }
    }

    if (nexta == null) {
        console.log("next a is null");
        return;
    }
    nexta.click();
}

function checkPlay() {
    let playing = document.getElementsByClassName("prism-big-play-btn playing");
    if (playing.length > 0) {
        console.log("playing");
        return;
    }

    let p = document.getElementsByClassName("prism-big-play-btn")[0] || document.getElementsByClassName("prism-big-play-btn pause")[0];
    if (p) {
        console.log("auto play");
        p.click();
    } else {
        console.log("cannot find play btn");
    }
}

function checkEnd() {
    let ds = document.getElementsByClassName("duration");
    if (ds == null || ds.length != 1) {
        console.log("ds error: " + ds);
        return false;
    }

    let ct = document.getElementsByClassName("current-time");
    if (ct == null || ct.length != 1) {
        console.log("ct error: " + ct);
        return false;
    }
    let cttime = convertTime(ct[0].innerHTML);
    let dstime = convertTime(ds[0].innerHTML);
    if ((cttime == dstime || cttime + 1 == dstime) && dstime != 0) {
        setTimeout(() => {
            learnNextClass();
        }, 1000);
        return true;
    } else {
        console.log(ct[0].innerHTML, ds[0].innerHTML);
    }
    return false;
}

var selected = false;
function checkQuestion() {
    if (selected) {
        checkConfirmQuestion();
        return true;
    }

    let wp = document.getElementsByClassName("panel window")[0];
    if (wp == null) return false;
    console.log("answer");
    if (wp.style.display != "none") {
        // if (document.getElementById("from_ejectque")) {
        let a = document.getElementById("adiv");
        let b = document.getElementById("bdiv");
        if (Math.random() > 0.5) {
            if (a && a.firstElementChild && a.firstElementChild.firstElementChild) {
                a.firstElementChild.firstElementChild.click();
                console.log("selec a");

                let rs = document.getElementsByClassName("reply-sub")[0];
                if (rs) {
                    selected = true;
                    rs.click();
                    console.log("submit a");
                }
                return true;
            }
        } else {
            if (b && b.firstElementChild && b.firstElementChild.firstElementChild) {
                b.firstElementChild.firstElementChild.click();
                console.log("selec b");

                let rs = document.getElementsByClassName("reply-sub")[0];
                if (rs) {
                    selected = true;
                    rs.click();
                    console.log("submit b");
                }
                return true;
            }
        }
    }
    return false;
}

function checkConfirmQuestion() {
    let okp = document.getElementsByClassName("dialog-button messager-button")[0];
    if (okp && okp.firstElementChild) {
        okp.firstElementChild.click();
    } else {
        selected = false;
    }
}

setInterval(() => {
    if (checkEnd()) {
        console.log("load next");
    } else {
        if (checkQuestion()) {
            console.log("answer question")
        } else {
            checkPlay();
        }
    }
}, 5000);
